﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sumen.Model.Model;

namespace Sumen.Business.Presenter.Category
{
    public class PositionXPresenter
    {


        public ICollection<position_x_inventory> GetAll()
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                return (from p in objEntities.position_x_inventory select p).ToList();
            }
        }



        public position_x_inventory GetById(object pId)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                int? id;
                if (pId == null || pId.ToString() == "")
                {
                    id = null;
                }
                else
                {
                    id = Convert.ToInt32(pId);
                }
                return objEntities.position_x_inventory.FirstOrDefault(p => p.Posx_ID == id);
            }
        }


        /// <summary>
        /// Get by Stock ID
        /// </summary>
        /// <param name="pValue"></param>
        /// <returns></returns>
        public ICollection<object> GetByKey(object pValue)
        {
            using (sumenEntities obj = new sumenEntities())
            {
                string id = pValue + string.Empty;
                var query = from p in obj.position_x_inventory
                            where p.Posx_STOCK_ID == id
                            select new
                            {
                                p.Posx_Code_X,
                                p.Posx_ID,
                                p.position_y_inventory.Posy_Code_Y,
                                p.position_range.Range_Code,
                                QuantityExist = p.Posx_Quantity_Max - p.Posx_Quantity
                            };
                var list = (from p in query.AsEnumerable() select p).ToList();
                return new[] { list };
            }
        }


    }
}
